package com.controller;
import com.dao.UserDao;
import com.pojo.Result;
import com.pojo.User;
import com.utils.SendEMailUtils;
import com.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import static com.utils.MessageUtils.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserDao userDao;

    @Autowired
    private SendEMailUtils sendEMailUtils;

    @GetMapping
    public Result getBYIdOrEMail(HttpServletRequest request){
        String userId =request.getSession().getAttribute("user").toString();
        User user = userDao.getBYIdOrEMail(userId);
        return Result.success(SELECT_DATA_SUCCESS,user);
    }

    @DeleteMapping
    public Result delete(HttpServletRequest request){
        int userId = (int) request.getSession().getAttribute("user");
        userDao.delete(userId);
        return Result.success(DELETE_DATA_SUCCESS);
    }

    @PutMapping
    public Result update(@RequestBody User user,HttpServletRequest request){
        int userId = (int) request.getSession().getAttribute("user");
        user.setId(userId);
        if (user.getLikeBook().charAt(0)!='《'){
            user.setLikeBook("《"+user.getLikeBook()+"》");
        }
        userDao.update(user);
        return Result.success(UPDATE_DATA_SUCCESS);
    }

    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Map<String,String> map, HttpServletRequest request){
        String userId = request.getSession().getAttribute("user").toString();
        map.put("id",userId);
        userDao.updatePassword(map);
        return Result.success(UPDATE_DATA_SUCCESS);
    }

    @GetMapping("/logout")
    public Result logout(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        request.getSession().removeAttribute("code");
        return Result.success(LOGOUT_SUCCESS);
    }

    @PostMapping("/quickLogin")
    public Result quickLogin(@RequestBody Map<String,String> map, HttpServletRequest request){
        String username = map.get("username");
        String password = map.get("password");
        Object validateCode=request.getSession().getAttribute("code");
        User user = userDao.getBYIdOrEMail(username);
        if (StringUtils.isEmpty(validateCode)){
            int count = userDao.getCount(map);
            if (count<=0){
                return Result.fail(LOGIN_ERROR);
            }
        }else {
            if (!password.equals(validateCode.toString())){
                return Result.fail(VALIDATE_CODE_ERROR);
            }
            if (user==null){
                userDao.quickAdd(username);
                user= userDao.getBYIdOrEMail(username);
            }
        }
        String name=user.getNickName();
        name=StringUtils.isEmpty(name)?user.getEMail():name;
        request.getSession().setAttribute("user",user.getId());
        return Result.success(LOGIN_SUCCESS,name);
    }

    @GetMapping("/sendEMail")
    public Result sendEMail(String eMail,HttpServletRequest request){
        Integer validateCode = ValidateCodeUtils.generateValidateCode(6);
        Boolean flag = sendEMailUtils.sendMail(eMail,validateCode);
        if (flag){
            request.getSession().setAttribute("code",validateCode);
            return Result.success(SEND_EMAIL_SUCCESS);
        }else {
            return Result.fail(SEND_EMAIL_ERROR);
        }
    }

    @GetMapping("/getEMail")
    public Result getEMail(HttpServletRequest request){
        String userId =request.getSession().getAttribute("user").toString();
        String eMail = userDao.getBYIdOrEMail(userId).getEMail();
        return Result.success(SELECT_DATA_SUCCESS,eMail);
    }
}
